﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
namespace HeavenvsDarkness
{
    public class MySprite : VisibleGameEntity
    {

        protected Texture2D[] texture;

        public Texture2D[] Texture
        {
            get { return texture; }
            set
            {
                texture = value;
                NTexture = texture.Count();
                Index = 0;
            }
        }
        protected int index;

        public int Index
        {
            get { return index; }
            set { index = value; }
        }
        protected int nTexture;

        public int NTexture
        {
            get { return nTexture; }
            set { nTexture = value; }
        }



        protected int timeDelay;

        public int TimeDelay
        {
            get { return timeDelay; }
            set { timeDelay = value; }
        }

        protected Color colorFilter;

        public Color ColorFilter
        {
            get { return colorFilter; }
            set { colorFilter = value; }
        }

        int speed;

        public int Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        public MySprite()
        {
            Items = new List<VisibleGameEntity>();
        }

      

        public MySprite(Texture2D[] _texture, Vector2 _Size, Vector3 _Position, int time)
        {
            Speed = 1;
            Items = new List<VisibleGameEntity>();
            Texture = _texture;
            NTexture = Texture.Count();
            Index = 0;
            TimeDelay = time;
            ColorFilter = Color.White;
            Position = _Position;
            Size = _Size;
        }
        public MySprite(XmlElement element)
        {
            Items = new List<VisibleGameEntity>();
            Vector3 _Position;
            int _time, _nTexture;
            String name;

            Scale = float.Parse(element.GetAttribute("Scale"));
            SpeedMove = float.Parse(element.GetAttribute("SpeedMove"));
            _Position.X = float.Parse(element.GetAttribute("X"));
            _Position.Y = float.Parse(element.GetAttribute("Y"));
            _Position.Z = float.Parse(element.GetAttribute("Z"));
            _time = Int16.Parse(element.GetAttribute("DelayTime"));
            _nTexture = Int16.Parse(element.GetAttribute("Count"));
            name = element.GetAttribute("Src");
            String shapeKind = element.GetAttribute("Shape");
            InitComponent(name, _nTexture, _Position, _time,shapeKind);
        }

        public void InitComponent(string name, int _nTexture, Vector3 _Position, int time, String shape)
        {
            Speed = 1;
            NTexture = _nTexture;
            Texture = new Texture2D[NTexture];

            for (int i = 0; i < NTexture; i++)
            {
                Texture[i] = GameUtil.game.Content.Load<Texture2D>(name + i.ToString("000"));

            }

            Size = new Vector2(Texture[0].Width, Texture[0].Height) * Scale;

            Index = 0;
            TimeDelay = time;
            ColorFilter = Color.White;
            Position = _Position;
            DestPost = Position;
            Rotation = Matrix.Identity;
            
            
            Model = new MyPlanarModel(Scale, Position, Rotation, MyPlanarModel.GetShape(shape));

        }
        override public void Update(GameTime gameTime)
        {
            Model.Position = Position;
            Model.Rotation = Rotation;
            Model.Scale = Scale;
            if (Speed == 0)
            {
                return;
            }

            if (DestPost != Position)
            {
                MoveTo(DestPost, SpeedMove);
            }
            int k = (int)gameTime.TotalGameTime.TotalMilliseconds / (TimeDelay / Speed);
            Index = (k) % NTexture;
            
        }



        public override void Draw(SpriteBatch spriteBatch)
        {
            Model.Draw(GetTexture());
        }

        public override int GetIndex()
        {
            return Index;
        }

        public override void SetIndex(int id)
        {
            Index = id;
        }


        public override Vector2 GetTextureSize()
        {
            return new Vector2(Texture[Index].Width, Texture[Index].Height);
        }

        public override Color[] GetTextureData()
        {
            Color[] data = new Color[(int)(GetTextureSize().X * GetTextureSize().Y)];
            Texture[Index].GetData(data);
            return data;
        }

        public override Color GetColorAtPos(float x, float y)
        {
            x = x - Position.X;
            y = y - Position.Y;

            float scale = GetTextureSize().X / Size.X;
            x = x * scale;
            y = y * scale;
            int indexOfData = (int)(x * (GetTextureSize().X) + y);
            if (x >= 0 && y >= 0)
            {
                Rectangle sourceRectangle = new Rectangle((int)x, (int)y, 1, 1);
                Color[] retrievedColor = new Color[1];
                Texture[Index].GetData<Color>(0, sourceRectangle, retrievedColor, 0, 1);
                return retrievedColor[0];
            }

            return new Color(0, 0, 0, 0);
        }

        public override Texture2D GetTexture()
        {
            return Texture[Index];
        }

        public override void SetScale(float scale)
        {
            base.SetScale(scale);
            Model.Scale = scale;
        }

    }
}
